# -*- coding: utf-8 -*-
import cpsserver
import win32api
import win32con
import time
from linecache import getline

server = cpsserver.CPSServer("../data")
panel = server.get_panel('panel_k_2')

server.start(4242)

def arm4(value):
    if value < -8:
        win32api.keybd_event(0x32, 0, 0, 0)
    elif value > -2:
        win32api.keybd_event(0x57, 0, 0, 0)
    else:
        win32api.keybd_event(0x32, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(0x57, 0, win32con.KEYEVENTF_KEYUP, 0)

def arm5(value):
    if value < -3:
        win32api.keybd_event(0x31, 0, 0, 0)
    elif value > 3:
        win32api.keybd_event(0x51, 0, 0, 0)
    else:
        win32api.keybd_event(0x31, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(0x51, 0, win32con.KEYEVENTF_KEYUP, 0)

def switch1(value):
    if value == 0:
        win32api.keybd_event(0xDD, 0, 0, 0)
        time.sleep(1)
        win32api.keybd_event(0xDD, 0, win32con.KEYEVENTF_KEYUP, 0)
        panel.set_value('lamp1', 1)
        panel.set_value('lamp2', 0)
    else:
        win32api.keybd_event(0xDB, 0, 0, 0)
        time.sleep(1)
        win32api.keybd_event(0xDB, 0, win32con.KEYEVENTF_KEYUP, 0)
        panel.set_value('lamp1', 0)
        panel.set_value('lamp2', 1)

def button1(value):
    if value == 0:
        win32api.keybd_event(0x35, 0, 0, 0)
    else:
        win32api.keybd_event(0x35, 0, win32con.KEYEVENTF_KEYUP, 0)

def button2(value):
    if value == 0:
        win32api.keybd_event(0x36, 0, 0, 0)
    else:
        win32api.keybd_event(0x36, 0, win32con.KEYEVENTF_KEYUP, 0)
        
panel.set_handler('arm4', arm4)
panel.set_handler('arm5', arm5)
panel.set_handler('switch1', switch1)
panel.set_handler('button1', button1)
panel.set_handler('button2', button2)

raw_input()

server.stop()